<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/add.css"/>

		<link rel="stylesheet" href="../../assets/css/layui.css">
		<link rel="stylesheet" href="../../assets/css/view.css"/>
		<script src="/qrcode/jquery2.1.4.js"></script>
		<script src="/layer/layer.js"></script>
	</head>
	<body>

	<div class="layui-content">
		<div class="layui-row">
			<div class="layui-card">
				<div class="layui-card-header">添加用户</div>
				<form id="form1" class="layui-form layui-card-body">


					<div class="layui-form-item">
						<label class="layui-form-label">姓名</label>
						<div class="layui-input-block">
							<input type="text" id="username" name="code" required lay-verify="required"
								   placeholder="请输入用户姓名"
								   autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">电话</label>
						<div class="layui-input-block">
							<input type="text" id="userphone" name="username" required lay-verify="required"
								   placeholder="请输入电话" autocomplete="off" class="layui-input">
						</div>
					</div>


					<div class="layui-form-item">
						<label class="layui-form-label">密码</label>
						<div class="layui-input-block">
							<input type="text" id="password" name="phonenumber" required lay-verify="required"
								   placeholder="请输入密码" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-blue" lay-submit lay-filter="formDemo">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>




	<script>

		function isName() {
			var username = $("input:eq(0)").val();
			if (username == "") {
				layer.tips("姓名不能为空", "#username", {tipsMore: true, tips: 1});
				return false;
			} else if (/^[\u4E00-\u9FA5A-Za-z0-9_]+$/.test(username) == false) {      //英文，中文，数字，下划线
				layer.tips("姓名只支持英文、中文、数字和下划线", "#username", {tipsMore: true, tips: 1});
				return false;
			}
			return true;
		}

		function isPhone() {
			var userphone = $("input:eq(1)").val();
			if (userphone == "") {
				layer.tips("电话不能为空", "#userphone", {tipsMore: true, tips: 1});
				return false;
			} else if (/^^1[0-9]{10}$$/.test(userphone) == false) {      //英文，中文，数字，下划线
				layer.tips("电话格式不合法", "#userphone", {tipsMore: true, tips: 1});
				return false;
			}
			return true;
		}



		function isPassword() {
			var password = $("input:eq(2)").val();
			if (password == "") {
				layer.tips("密码不能为空", "#password", {tipsMore: true, tips: 1});
				return false;
			} else if (password.length < 6) {      //英文，中文，数字，下划线
				layer.tips("密码位数不能少于6位", "#password", {tipsMore: true, tips: 1});
				return false;
			}
			return true;
		}

	</script>
	<script>
		$(function () {
			$("input:eq(0)").blur(function () {
				isName();
			});

			$("input:eq(1)").blur(function () {
				isPhone();
			});


			$("input:eq(2)").blur(function () {
				isPassword();
			});


			$("#form1").submit(function () {
				var bool = isName() && isPhone() && isPassword();
				if (bool) {
					var windowId = layer.load();
					var userName = $("input:eq(0)").val();
					var userPhone = $("input:eq(1)").val();
					var password = $("input:eq(2)").val();
					$.post("/user/insert.do", {
						userName: userName,
						userPhone: userPhone,
						password: password
					}, function (data) {
						layer.close(windowId);
						layer.msg(data.result);
						if (data.status == 0) {
							//录入成功
							$("input").val("");
						} else {
							//录入失败
						}
					});

					return false;
				}
			});


		})
	</script>



	</body>
</html>
